#!/bin/sh
# 接收传入的数组并遍历
operation_type=$1
jvmParams=&jvmParamsPro
profiles=&profilesPro

echo "jvmParams=${jvmParams}"
echo "profiles=${profiles}"

WORK_DIR=$(cd $(dirname $0); pwd)
apppath="${WORK_DIR}"
appurl="${WORK_DIR}/app.jar"
echo $operation_type $appurl "..."

is_exist(){
  pid=`ps -ef|grep $appurl|grep -v grep|awk '{print $2}' `
  #如果不存在返回1，存在返回0
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}

start(){
	is_exist
  	if [ $? -eq "0" ]; then
    		echo "${APP_NAME} is already running. pid=${pid} ."
  	else
		echo  "nohup java ${jvmParams} -jar $appurl ${profiles}  > $apppath/app.log 2>&1 &"
		nohup java ${jvmParams} -jar $appurl ${profiles}  > $apppath/app.log 2>&1 &
		echo "${appurl} start success"
	fi
}

run(){
	echo  "java ${jvmParams} -jar $appurl ${profiles}  > $apppath/app.log 2>&1 && tail -f -n100 $apppath/app.log"
	java ${jvmParams} -jar $appurl ${profiles}  > $apppath/app.log 2>&1 &
	tail -f -n100 $apppath/app.log
}

#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${appurl} is running. Pid is ${pid}"
  else
    echo "${appurl} is NOT running."
  fi
}


stop(){
	#echo  "ps -ef | grep $appurl | grep -v grep | awk '{print \$2}' | xargs kill -9"
	#ps -ef | grep $appurl | grep -v grep | awk '{print $2}' | xargs kill -9
	is_exist
	if [ $? -eq "0" ]; then
    		kill -9 $pid
  	else
    		echo "${appurl} is not running"
  	fi
}

restart(){
	stop
  	start
}

logs(){
	tail -f -n100 $apppath/app.log
}

case "$operation_type" in
  start)
    start
    ;;
  run)
    run
    ;;
  status)
    status
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  logs)
    logs
    ;;
  *)
    echo "Usage: $0 {start|stop|run|status|restart|logs}"
    exit 1
esac

exit 0
